package com.bawei.module_main

import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_mvi.Router
import com.bawei.lib_mvi.base.BaseMVIActivity
import com.bawei.module_home.ui.HomeFragment
import com.bawei.module_live.ui.LiveFragment
import com.bawei.module_main.adapter.MainViewPageAdapter
import com.bawei.module_main.databinding.ActivityMainBinding
import com.bawei.module_mine.ui.MineFragment
import com.bawei.module_video_hall.ui.VideoHallFragment
import com.bawei.module_live.ui.liveDateil.LiveDateilActivity
import kotlinx.coroutines.*


@Route(path = Router.Ui.MainActivity)
class MainActivity : BaseMVIActivity() {
    private val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }
    
    // 延迟初始化Fragment列表
    private var fragments: MutableList<Fragment>? = null
    private lateinit var adapter: MainViewPageAdapter
    
    override fun generateViewModel() {

    }

    override fun getLayoutRootView(): View {
        return binding.root
    }

    override fun handleState() {

    }

    override fun initView() {
        super.initView()
        // 先初始化基本UI，Fragment延迟初始化
        initBasicUI()
    }

    override fun initData() {
        super.initData()
        
        // 延迟初始化Fragment，提升启动速度
        lifecycleScope.launch {
            initFragments()
        }

    }

    private fun initBasicUI() {
        // 设置ViewPager2的基本配置
        binding.vp.offscreenPageLimit = 2
        binding.vp.isUserInputEnabled = false
        
        // 设置底部导航栏
        binding.bottom.setOnItemSelectedListener {
            // 先彻底释放，再兜底静音
            (fragments?.getOrNull(2) as? LiveFragment)?.releaseAllPlayersSafe()
            (fragments?.getOrNull(2) as? LiveFragment)?.muteAllPlayersSafe()
            val position = when (it.itemId) {
                R.id.HomeFragment -> 0
                R.id.VideoHallFragment -> 1
                R.id.LiveFragment -> 2
                R.id.MineFragment -> 3
                else -> 0
            }
            binding.vp.setCurrentItem(position, true)
            true
        }
        
        binding.fabCenter.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.PUBLISH_Actvivty).navigation()
        }
    }

    private suspend fun initFragments() {
        withContext(Dispatchers.Main) {
            // 延迟初始化Fragment
            fragments = mutableListOf(
                HomeFragment(),
                VideoHallFragment(),
                LiveFragment(),
                MineFragment()
            )
            
            adapter = MainViewPageAdapter(this@MainActivity, fragments!!)
            binding.vp.adapter = adapter
            
            // 设置页面切换回调
            binding.vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    binding.bottom.selectedItemId = when (position) {
                        0 -> R.id.HomeFragment
                        1 -> R.id.VideoHallFragment
                        2 -> R.id.LiveFragment
                        3 -> R.id.MineFragment
                        else -> R.id.HomeFragment
                    }
                }
            })
        }
    }
}